package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.CourseCategoryTreeDto;
import com.xuecheng.content.model.dto.CourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<CourseTeacher> getCourseTeacher(Long courseId) {
        //构建查询条件对象 LambdaQueryWrapper相比于queryWrapper来说更安全，是他的子类
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId);
        List<CourseTeacher> courseTeacherList = courseTeacherMapper.selectList(queryWrapper);
        return courseTeacherList;
    }

    @Override
    public CourseTeacher addCourseTeacher(CourseTeacherDto dto) {
        CourseTeacher courseTeacherNew = new CourseTeacher();
        BeanUtils.copyProperties(dto,courseTeacherNew);
        courseTeacherNew.setCreateDate(LocalDateTime.now());
        Long id = dto.getId();
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(id);
        //如果表内无重复id则直接添加
        if(courseTeacher == null){
            int insert = courseTeacherMapper.insert(courseTeacherNew);
            if(insert<=0){
                throw new XueChengPlusException("新增课程教师信息失败");
            }
        }else{
            //如果有重复的值则更新当前数据
            courseTeacherMapper.updateById(courseTeacherNew);
        }
        return courseTeacherNew;
    }

    @Override
    public void delCourseTeacher(Long courseId, Long teacherId) {
        if (courseId != null && teacherId != null){
            courseTeacherMapper.delCourseTeacher(courseId,teacherId);
        }
    }


}
